package com.pubpage.flow.flow_note_add;

import javax.servlet.http.HttpServletRequest;
import com.pub.database.QueryBean;

public class Action {
    QueryBean gqbnDAO=null;
    public String action(javax.servlet.jsp.PageContext pageContext){
       HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
       //HttpServletResponse response=(HttpServletResponse)pageContext.getResponse();

       View view=(View)request.getAttribute("view");

       String lstrUserNO = null;

       gqbnDAO=new QueryBean(view.getDb_alias(),view.getDb_debug(),view.getSource_charset(),view.getTarget_charset());

       Business business=new Business(gqbnDAO);

       String lstrResult=null;

       try{
           if(view.getPrgAction()!=null && view.getPrgAction().equals("getData")){//利用AJAX即時讀取資料
               lstrResult=business.getData(view,lstrUserNO);

               pageContext.getOut().clear();
               pageContext.getOut().write(lstrResult);
               pageContext.getOut().flush();
               return "login";
           }

           business.initAction(view,lstrUserNO);

           if(view.getPrgAction()!=null && view.getPrgAction().equals("confirm")){
               business.saveAction(view,lstrUserNO);
           }

           business.setButtonShow(view,view.getPrgAction(),view.getPrgStatus(),lstrUserNO);
           business.setReadonlyFields(view,view.getPrgAction(),view.getPrgStatus(),lstrUserNO);
           business.setShowFields(view,view.getPrgAction(),view.getPrgStatus(),lstrUserNO);

          return "";
       }
       catch(Exception e){
           if(view.getDb_debug()){
               e.printStackTrace();
           }
           view.setOnloadScript("alert('" + e.toString() + ", please contact IT.');");
           return "";
       }
       finally{
          try{
             if(gqbnDAO!=null){
                gqbnDAO.close();
             }
          }
          catch(Exception e){
             System.out.println("can not close querybean in Action.java");
          }
       }
   }
}
